package com.zhida.smartcontrol.launch.login;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.jay.tools.utils.SharedUtil;
import com.yanzhenjie.kalle.Params;
import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.Keys;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.launch.login.bean.Login;
import com.zhida.smartcontrol.launch.login.bean.LoginBean;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;
import com.zhida.smartcontrol.pagectrl.ProjectListBean;

import java.util.List;

/**
 * author: tanjianpei
 * date: 2019/8/19 019 15:08
 * description:
 */
public class LoginPresenter extends BasePresenterImpl<LoginContract.View> implements LoginContract.Presenter {

    @Override
    public void login(String userName, String passWord) {
        if (TextUtils.isEmpty(userName)) {
            mView.showMsg("请输入手机号码");
            return;
        }
        if (TextUtils.isEmpty(passWord)) {
            mView.showMsg("请输入密码");
            return;
        }

        SharedUtil.putString(Keys.INPUT_USER, userName);
        SharedUtil.putString(Keys.INPUT_PASS, passWord);

        mKalleCenter.postJson(Api.LOGIN, new Login(userName, passWord), true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                SharedUtil.putString(Keys.USER_INFO, result);

                LoginBean bean = JSON.parseObject(result, LoginBean.class);

                SharedUtil.putString(Keys.TOKEN, bean.getToken());

                Params params = Params.newBuilder()
                        .add("projectName", "")
                        .build();
                mKalleCenter.get(Api.PROJECT_LIST, params, true, new KalleCallBack() {
                    @Override
                    public void onSuccess(String result, String msg) {
                        SharedUtil.putString(Keys.PROJECT_LIST, result);
                        if (null != mView)
                            mView.loginOk();
                    }

                    @Override
                    public void onFailed(String msg) {
                        mView.showMsg(msg);
                    }
                });

            }

            @Override
            public void onFailed(String msg) {
                if (null != mView)
                    mView.showMsg(msg);
            }
        });
    }
}
